<script lang="ts">
export default {
  name: 'subuserList',
  description: '企业子用户管理',
};
</script>
<script setup lang="ts">
import { onMounted, reactive, ref, toRefs ,defineEmits} from 'vue';

import { ElForm, ElMessage } from 'element-plus';
import { SubuserType, SubuserTypeQuery } from '@/api/occupant/subuser/types';
import {
  casualEntrepreneurAddItem,
  casualEntrepreneurUpdateItem,
} from '@/api/occupant/subuser';
import { casualEntrepreneurOptions } from '@/api/occupant/enterprise';

const dataFormRef = ref(ElForm);
const props = defineProps({
    dialog: Boolean,
    title:String,
    params:{
      default:()=>{},
      type:Object
    }
});
const emit = defineEmits(['update:dialog','entrepreneurItempages'])
const state = reactive({
  loading: false,
  // 选中ID数组
  ids: [],
  deptOptions: [],
  dataList: [] as SubuserType[],
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  } as SubuserTypeQuery,
  formData: {} as SubuserType,
  rules: {
    // entrepreneurId: [{ required: true, message: '请选择企业用户', trigger: 'blur' }],
    accountName: [
      { required: true, message: '请填写账户名称', trigger: 'blur' },
    ],
    nickname: [
      { required: true, message: '请填写昵称', trigger: 'blur' },
    ],
    password: [{ required: true, validator: validatePassword }],
  },
  total: 0,
  casualEntrepreneurOption: [] as OptionType[],
});
function validatePassword(rule: any, value: any, callback: any) {
  console.log('试试');

  let reg =  /^(?![A-z0-9]+$)(?=.[^%&',;=?$\x22])(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{8,20}$/
  if (!reg.test(value)) {
    callback(new Error('密码由8位数字、大小写字母和特殊符号组成!'));
  } else {
    callback();
  }
}
const {
  formData,
  rules,
  casualEntrepreneurOption,
} = toRefs(state);
function getOptions() {
  casualEntrepreneurOptions().then((res) => {
    casualEntrepreneurOption.value = res.data;
  });
}

/**
 * 确认
 */
function handleSubmit() {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      let parmas = Object.assign({}, formData.value);
      parmas.entrepreneurId = props.params.id
      // if (formData.value.id) {
      //  casualEntrepreneurUpdateItem(parmas).then(() => {
      //    ElMessage.success('编辑成功');
      //    closeDialog();
      //  });
      // } else {
      casualEntrepreneurAddItem(parmas).then(() => {
        ElMessage.success('新增成功');
        closeDialog();
        emit('entrepreneurItempages',props.params.id)
      });
      // }
    }
  });
}
/**
 * 关闭弹窗
 **/
function closeDialog() {
  emit('update:dialog',false)
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
}

onMounted(() => {
  getOptions();
});
</script>
<template>
  <!-- dialog -->
  <el-dialog :title="props.title" :model-value="props.dialog" width="500px" @close="closeDialog" append-to-body>
    <el-form ref="dataFormRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="企业用户" prop="entrepreneurId">
        <!-- <el-select v-model="formData.entrepreneurId" placeholder="请选择企业用户">
          <el-option v-for="item in casualEntrepreneurOption" :key="item.value" :label="item.label" :value="item.value" />
        </el-select> -->
        <el-input :value="props.params.name" placeholder="" disabled></el-input>
      </el-form-item>
      <el-form-item label="账户名称" prop="accountName">
        <el-input v-model="formData.accountName" placeholder="请填写账户名称"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="formData.nickname" placeholder="请填写昵称"></el-input>
      </el-form-item>
      <el-form-item label="账户密码" prop="password">
        <el-input v-model="formData.password" placeholder="请填写账户密码"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
</style>
